package com.eoner.uikit.edits;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;

import com.eoner.baselib.utils.toast.ToastUtil;

/**
 * <pre>
 *     Copyright    : Copyright (c) 2020.
 *     Authour      : hxm.
 *     Created Time : 2020/12/30.
 *     Desc         :
 *     Alter By     :
 * </pre>
 */
@SuppressLint("AppCompatCustomView")
public class MyEditView extends EditText {

    private ClipSuccessListener clipSuccessListener;

    public MyEditView(Context context) {
        super(context);
    }

    public MyEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        if (id == android.R.id.paste) {
            ClipboardManager clip = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            //改变剪贴板中Content
            String clipTxt = clip.getText().toString().replace(" ","");
            if (clipTxt.length() >= 6){
                if (!isNumeric(clipTxt)) {
                    ToastUtil.showToast("非纯数字，无法粘贴");
                    return false;
                }
                clipSuccessListener.onClipSuccess(clipTxt);
                return true;
            }
        }
        return false;
    }

    private boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim()))
            return s.matches("^[0-9]*$");
        else
            return false;
    }

    public void setOnClipSuccessListener(ClipSuccessListener clipSuccessListener){
        this.clipSuccessListener =clipSuccessListener;
    }

    interface ClipSuccessListener{
        void onClipSuccess(String clipTxt);
    }
}
